package com.cn.dream_order_server.service.impl;

import com.cn.dream_order_server.async.*;
import com.cn.dream_order_server.client.UserSiteServer;
import com.cn.dream_order_server.mapper.*;
import com.cn.dream_order_server.pojo.*;
import com.cn.dream_order_server.service.OrderOrderService;
import com.cn.dream_order_server.service.OrderOrderdetailsService;
import com.cn.dream_order_server.util.DoCreatOrderUtil;
import com.cn.dream_order_server.util.JsonUtils;
import com.cn.dream_order_server.util.RandomGenUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

@Service
public class OrderOrderServiceImpl implements OrderOrderService {

    @Autowired
    private OrderOrderMapper orderOrderMapper;

    @Autowired
    private OrderPaytypeMapper orderPaytypeMapper;

    @Autowired
    private OrderPeymodeMapper orderPeymodeMapper;

    @Autowired
    private OrderOrdertypeMapper orderOrdertypeMapper;


    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private OrderLogisticsMapper orderLogisticsMapper;

    @Autowired
    private OrderLogisticsstatusMapper orderLogisticsstatusMapper;


    @Autowired
    private WlcOrderOrderAsync orderOrderAsync;

    @Autowired
    private DeliverGoodsAsync deliverGoodsAsync;

    @Autowired
    private UserSiteServer userSiteServer;








    @Override
    public PageInfo<OrderOrder> getOrderOrderListByUserId(String userId,Integer pageNo,Integer PageSize) {





        String str = stringRedisTemplate.opsForValue().get("orderOrderTotal-" + userId + "-" + pageNo+"-"+PageSize);
        PageInfo<OrderOrder> pageInfo = null;
        if (str != null  && !"".equals(str)){
            pageInfo= JsonUtils.stringToObj(str, PageInfo.class);


        }else {
            OrderOrderExample example = new OrderOrderExample();
            OrderOrderExample.Criteria criteria = example.createCriteria();
            criteria.andUserIdEqualTo(userId);
            criteria.andOrdertypeIdNotEqualTo("5");
            List<OrderOrder> list = orderOrderMapper.selectByExample(example);
            for (OrderOrder orderOrder:list ) {
                orderOrder.setOrderOrdertype(orderOrdertypeMapper.selectByPrimaryKey(orderOrder.getOrdertypeId()));
                String o = userSiteServer.getUserSiteBySiteId(orderOrder.getSiteId());
                System.out.println(o);
                orderOrder.setUserSite(JsonUtils.stringToObj(o,UserSite.class));
                orderOrder.setOrderPeymode(orderPeymodeMapper.selectByPrimaryKey(orderOrder.getPayMode()));
                orderOrder.setOrderPaytype(orderPaytypeMapper.selectByPrimaryKey(orderOrder.getPaytypeId() ));
            }
            pageInfo =new PageInfo<OrderOrder>(list);
        }

        return pageInfo;
    }

    @Override
    public PageInfo<OrderOrder> getOrderOrderListByUserIdAndOrdertypeId(String userId,String ordertypeId,Integer pageNo,Integer PageSize){



        String str = null;
        switch (ordertypeId){
            case "1":
                str =  stringRedisTemplate.opsForValue().get("orderOrderPayment-" + userId + "-" + pageNo+"-"+PageSize);
                break;
            case "2":
                str =  stringRedisTemplate.opsForValue().get("orderOrderWaitForReceiving-" + userId + "-" + pageNo+"-"+PageSize);
                break;
            case "3":
                str =  stringRedisTemplate.opsForValue().get("orderOrderAccomplish-" + userId + "-" + pageNo+"-"+PageSize);
                break;
            case "4":
                str =   stringRedisTemplate.opsForValue().get("orderOrderCancel-" + userId + "-" + pageNo+"-"+PageSize);
                break;
        }
        PageInfo<OrderOrder> pageInfo = null;
        if (str != null  && !"".equals(str)){
            pageInfo= JsonUtils.stringToObj(str, PageInfo.class);


        }else {
            OrderOrderExample example = new OrderOrderExample();
            OrderOrderExample.Criteria criteria = example.createCriteria();
            criteria.andUserIdEqualTo(userId);
            criteria.andOrdertypeIdEqualTo(ordertypeId);
            List<OrderOrder> list = orderOrderMapper.selectByExample(example);
            for (OrderOrder orderOrder:list ) {
                orderOrder.setOrderOrdertype(orderOrdertypeMapper.selectByPrimaryKey(orderOrder.getOrdertypeId()));
                String o = userSiteServer.getUserSiteBySiteId(orderOrder.getSiteId());
                orderOrder.setUserSite(JsonUtils.stringToObj(o,UserSite.class));
                orderOrder.setOrderPeymode(orderPeymodeMapper.selectByPrimaryKey(orderOrder.getPayMode()));
                orderOrder.setOrderPaytype(orderPaytypeMapper.selectByPrimaryKey(orderOrder.getPaytypeId() ));
            }
            pageInfo = new PageInfo<OrderOrder>(list);
        }

        return pageInfo;
    }

    @Override
    public Integer doConfirmReceipt(String orderId, Integer pageNo, Integer thisPage, String userId) {
            //根据订单id获取订单信息
        OrderOrder orderOrder = orderOrderMapper.selectByPrimaryKey(orderId);
        //判断订单信息是否为空
        if (orderOrder!=null){
            //根据物流id获取物流信息
            OrderLogistics orderLogistics = orderLogisticsMapper.selectByPrimaryKey(orderOrder.getOrderOther1());
            //判断物流信息是否为空
            if (orderLogistics!= null){
                //判断状态id是否为4已完成
                if (orderLogistics.getLogisticsstatusId() .equals("4")){
                    orderOrderAsync.orderSignAsync(orderId,pageNo,thisPage,userId);
                    orderOrder.setOrdertypeId("3");
                    orderOrder.setOrderOther4(1);
                    orderLogistics.setDeliveryTime(new Date());
                    //异步执行方法
                    deliverGoodsAsync.doDeliveryTime(orderLogistics);
                    return orderOrderMapper.updateByPrimaryKeySelective(orderOrder);
                }
            }
        }


        return null;
    }

    @Override
    public Integer doDeleteShoppingOrder(String orderId, Integer pageNo, Integer thisPage, String userId) {
        //根据订单id获取订单信息
        OrderOrder orderOrder = orderOrderMapper.selectByPrimaryKey(orderId);
        //判断订单信息是否为空
        if (orderOrder!=null){
             //请求异步操作
             orderOrderAsync.orderDeleteAsync(orderId,pageNo,thisPage,userId);
             //设置订单状态为已删除
             orderOrder.setOrdertypeId("5");
             //修改到数据库
             return orderOrderMapper.updateByPrimaryKeySelective(orderOrder);


        }


        return null;
    }



    @Override
    public Integer doConfirmReceiptManage(String orderId) {

        //根据订单id获取订单信息
        OrderOrder orderOrder = orderOrderMapper.selectByPrimaryKey(orderId);
        //判断订单信息是否为空
        if (orderOrder!=null){
            //根据物流id获取物流信息
            OrderLogistics orderLogistics = orderLogisticsMapper.selectByPrimaryKey(orderOrder.getOrderOther1());
            //判断物流信息是否为空
            if (orderLogistics!= null){
                //判断状态id是否为4已完成
                if (orderLogistics.getLogisticsstatusId() .equals("4")){
                    orderOrder.setOrdertypeId("3");
                    orderOrder.setOrderOther4(1);
                    stringRedisTemplate.delete("manage-"+orderId);
                    System.out.println("成功！");
                    orderLogistics.setDeliveryTime(new Date());
                    return orderOrderMapper.updateByPrimaryKeySelective(orderOrder);
                }
            }
        }


        return null;
    }



    @Override
    public Integer doOrderCallOff(OrderOrder orderOrder) {
        //订单状态修改为取消
        orderOrder.setOrderOther4(5);
        orderOrder.setOrdertypeId("4");
        //调用方法进行修改
        return orderOrderMapper.updateByPrimaryKeySelective(orderOrder);
    }


}
